package com.breeze.hllzdjd;

import android.app.Notification;
import android.app.PendingIntent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class JDNotificationService extends NotificationListenerService {
    private static final String TAG = "JDNotificationService";

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Log.i(TAG, "onNotificationPosted: " + sbn.getPackageName());
        if (!Constants.HLL_PACKAGE_NAME.equals(sbn.getPackageName())) {
            return;
        }
        Notification notification = sbn.getNotification();
        Bundle extras = notification.extras;
        // String tickerText = (String) notification.tickerText; 同时包含title和content
        String content = extras.getString(Notification.EXTRA_TEXT);
        if (isHLLDD(content)) {
            openQDPage(notification);
        }

    }

    private boolean isHLLDD(String content) {
        for (String s : Constants.KEY_JD_TEXT.split(",")) {
            if (content.contains(s)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 打开抢单界面
     *
     * @param notification
     */
    private void openQDPage(Notification notification) {
        PendingIntent pendingIntent = notification.contentIntent;
        try {
            pendingIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.i(TAG, "onListenerConnected: ");
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
        Log.i(TAG, "onListenerDisconnected: ");
    }
}
